﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AjaxPro;
using System.Data;
using System.IO;
using System.Web;
using System.Configuration;
namespace Common
{
    public class ResourceBLL
    {
        // Methods
        [AjaxMethod]
        public static string GetFrontEndText(string Site, string ServiceId, string Id, string DefaultText)
        {
            DataSet set;
            Directory.CreateDirectory(PathRoot + @"\ResourceManager");
            Directory.CreateDirectory(PathRoot + @"\ResourceManager\sites");
            Directory.CreateDirectory(PathRoot + @"\ResourceManager\sites\" + Site);
            Directory.CreateDirectory(PathRoot + @"\ResourceManager\sites\" + Site + @"\" + ActiveLanguage);
            string fileName = PathRoot + @"\ResourceManager\sites\" + Site + @"\" + ActiveLanguage + @"\" + ServiceId + ".xml";
            FileInfo info = new FileInfo(fileName);
            if (!info.Exists)
            {
                set = new DataSet("Resources");
                set.Tables.Add("Languages");
                set.Tables[0].Columns.Add("Id");
                set.Tables[0].Columns.Add("Value");
                set.Tables[0].Rows.Add(new object[] { Id, DefaultText });
                set.WriteXml(fileName);
                return DefaultText;
            }
            set = new DataSet("Resources");
            set.ReadXml(fileName);
            DataView view = new DataView(set.Tables[0], "Id='" + Id + "'", "ID ASC", DataViewRowState.CurrentRows);
            if (view.Count == 0)
            {
                set.Tables[0].Rows.Add(new object[] { Id, DefaultText });
                set.WriteXml(fileName);
                return DefaultText;
            }
            return (string)view[0]["Value"];
        }

        [AjaxMethod]
        public static string GetGlobalText(string ServiceId, string Id, string DefaultText)
        {
            DataSet set;
            Directory.CreateDirectory(PathRoot + @"\ResourceManager");
            Directory.CreateDirectory(PathRoot + @"\ResourceManager\global");
            Directory.CreateDirectory(PathRoot + @"\ResourceManager\global\" + ActiveLanguage);
            string fileName = PathRoot + @"\ResourceManager\global\" + ActiveLanguage + @"\" + ServiceId + ".xml";
            FileInfo info = new FileInfo(fileName);
            if (!info.Exists)
            {
                set = new DataSet("Resources");
                set.Tables.Add("Languages");
                set.Tables[0].Columns.Add("Id");
                set.Tables[0].Columns.Add("Value");
                set.Tables[0].Rows.Add(new object[] { Id, DefaultText });
                set.WriteXml(fileName);
                return DefaultText;
            }
            set = new DataSet("Resources");
            set.ReadXml(fileName);
            DataView view = new DataView(set.Tables[0], "Id='" + Id + "'", "ID ASC", DataViewRowState.CurrentRows);
            if (view.Count == 0)
            {
                set.Tables[0].Rows.Add(new object[] { Id, DefaultText });
                set.WriteXml(fileName);
                return DefaultText;
            }
            return (string)view[0]["Value"];
        }

        [AjaxMethod]
        public static string GetText(string ServiceId, string Id, string DefaultText)
        {
            DataSet set;
            Directory.CreateDirectory(PathRoot + @"\ResourceManager");
            Directory.CreateDirectory(PathRoot + @"\ResourceManager\backend");
            Directory.CreateDirectory(PathRoot + @"\ResourceManager\backend\" + ActiveLanguage);
            string fileName = PathRoot + @"\ResourceManager\backend\" + ActiveLanguage + @"\" + ServiceId + ".xml";
            FileInfo info = new FileInfo(fileName);
            if (!info.Exists)
            {
                set = new DataSet("Resources");
                set.Tables.Add("Languages");
                set.Tables[0].Columns.Add("Id");
                set.Tables[0].Columns.Add("Value");
                set.Tables[0].Rows.Add(new object[] { Id, DefaultText });
                set.WriteXml(fileName);
                return DefaultText;
            }
            set = new DataSet("Resources");
            set.ReadXml(fileName);
            DataView view = new DataView(set.Tables[0], "Id='" + Id + "'", "ID ASC", DataViewRowState.CurrentRows);
            if (view.Count == 0)
            {
                set.Tables[0].Rows.Add(new object[] { Id, DefaultText });
                set.WriteXml(fileName);
                return DefaultText;
            }
            return (string)view[0]["Value"];
        }

        // Properties
        public static string ActiveLanguage
        {
            get
            {
                if (HttpContext.Current.Session["ActiveLanguage"] == null)
                {
                    HttpContext.Current.Session["ActiveLanguage"] = "vie";
                }
                return (string)HttpContext.Current.Session["ActiveLanguage"];
            }
            set
            {
                HttpContext.Current.Session["ActiveLanguage"] = value;
            }
        }

        public static string PathRoot
        {
            get
            {
                return Utilities.PhysicalApplicationPath();
            }
        }
    }


}
